/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is ThumbsDown.
 *
 * The Initial Developer of the Original Code is
 * Atte Kemppilä.
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */


function MatcherPage() // extend WizardPage
{
    let depends = "selected linkUrls images script";
    WizardPage.call(this, depends);
}

MatcherPage.prototype =
{
    onPageShow: function()
    {
        let self = this;
        
        if (this.firstRun)
        {
            this.$thumbnail = document.getElementById("tbox_matcher_thumbnail");
            this.$link = document.getElementById("tbox_matcher_link");
            this.$thumbnailResetDefault = document.getElementById("btn_matcher_thumbnail_resetDefault");
            this.$thumbnailResetScript = document.getElementById("btn_matcher_thumbnail_resetScript");
            this.$linkResetDefault = document.getElementById("btn_matcher_link_resetDefault");
            this.$linkResetScript = document.getElementById("btn_matcher_link_resetScript");
            this.$thumbnailList = document.getElementById("rlbox_matcher_thumbnail");
            this.$linkList = document.getElementById("rlbox_matcher_link");
        }
        
        if (this.dirty.selected || this.dirty.linkUrls || this.dirty.images)
        {
            this.entries =
                scriptWizard.getEntries({
                    thumbnail: model.selected,
                    linkUrl: model.linkUrls,
                    image: model.images,
                });
            
            clearListbox(this.$thumbnailList);
            clearListbox(this.$linkList);
            
            for each (let entry in this.entries)
            {
                this.$thumbnailList.appendItem(entry.thumbnailUrl);
                this.$linkList.appendItem(entry.linkUrl);
            }
        }
        
        if (this.dirty.selected || this.dirty.script)
        {
            let thumbnailUrls = [entry.thumbnailUrl for each (entry in this.entries)];
            let thumbnailUrlTokens = scriptWizard.getUrlTokens(thumbnailUrls);
            this.thumbnail = {};
            this.thumbnail.default = scriptWizard.resolveUrlRegExp(thumbnailUrlTokens, false);
            this.thumbnail.script = ((model.script && model.script.thumbnailPattern) ? model.script.thumbnailPattern.pattern : "");
            
            // linkType != "normal" -> linkUrl is expensive to resolve -> prefer thumbnail pattern
            if (model.script)
                this.$thumbnail.value = this.thumbnail.script;
            else if (model.linkType != "normal")
                this.$thumbnail.value = this.thumbnail.default;
            else
                this.$thumbnail.value = "";
            
            this.$thumbnailResetScript.hidden = (! model.script);
        }
        
        if (this.dirty.linkUrls || this.dirty.script)
        {
            let linkUrls = [entry.linkUrl for each (entry in this.entries)];
            let linkUrlTokens = scriptWizard.getUrlTokens(linkUrls);
            this.link = {};
            this.link.default = scriptWizard.resolveUrlRegExp(linkUrlTokens, false);
            this.link.script = ((model.script && model.script.linkPattern) ? model.script.linkPattern.pattern : "");
            
            // linkType == "normal" -> linkUrl is cheap to resolve -> prefer link pattern
            if (model.script)
                this.$link.value = this.link.script;
            else if (model.linkType == "normal")
                this.$link.value = this.link.default;
            else
                this.$link.value = "";
            
            this.$linkResetScript.hidden = (! model.script);
        }
        
        if (this.dirty.page)
        {
            this._updateButtons();
            this._updatePattern("thumbnail");
            this._updatePattern("link");
            
            if ((! this.$thumbnailList.disabled) && (! this.$thumbnailList.match))
                this.$thumbnail.focus();
            else if ((! this.$linkList.disabled) && (! this.$linkList.match))
                this.$link.focus();
            else if (this.$thumbnail.value)
                this.$thumbnail.focus();
            else
                this.$link.focus();
        }
    },
    
    onPatternInput: function(type)
    {
        // update buttons immediately so that textbox content and buttons are in sync
        this._updateButtons();
        // canAdvance() depends on textbox's timer
        callLater(0, null, function() { wizard.updateWizardButtons() });
    },
    
    onPatternCommand: function(type)
    {
        if (page != pages["matcher"])
            return;
        
        this._updatePattern(type);
        this._updateButtons();
        wizard.updateWizardButtons();
    },
    
    resetPattern: function(type, source)
    {
        document.getElementById("tbox_matcher_" + type).value = this[type][source];
        
        this._updatePattern(type);
        this._updateButtons();
        wizard.updateWizardButtons();
    },
    
    _updateButtons: function()
    {
        this.$thumbnailResetDefault.disabled = (this.$thumbnail.value == this.thumbnail.default);
        this.$thumbnailResetScript.disabled = (this.$thumbnail.value == this.thumbnail.script);
        this.$linkResetDefault.disabled = (this.$link.value == this.link.default);
        this.$linkResetScript.disabled = (this.$link.value == this.link.script);
    },
    
    _updatePattern: function(type)
    {
        // disable listbox if pattern is empty
        let patternValue = document.getElementById("tbox_matcher_" + type).value;
        let pattern = "^" + patternValue + "$";
        document.getElementById("rlbox_matcher_" + type).pattern = pattern;
        document.getElementById("rlbox_matcher_" + type).disabled = (! patternValue);
    },
    
    canAdvance: function()
    {
        // textbox input is pending
        if (this.$thumbnail._timer || this.$link._timer)
            return false;
        
        let thumbnailPattern = this.$thumbnail.value;
        let linkPattern = this.$link.value;
        
        // either thumbnail or link pattern must be set (or both)
        if ((thumbnailPattern.length == 0) && (linkPattern.length == 0))
            return false;
        
        if (thumbnailPattern && (! this.$thumbnailList.match))
            return false;
        
        if (linkPattern && (! this.$linkList.match))
            return false;
        
        return true;
    },
    
    onPageAdvanced: function()
    {
        model.thumbnailPattern = this.$thumbnail.value || null;
        model.linkPattern = this.$link.value || null;
        
        if (model.type == "direct")
            return wizard.advance("script");
        
        return wizard.advance("type");
    }
}

extend(MatcherPage, WizardPage);

pages["matcher"] = new MatcherPage();
